﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;

namespace WXYW
{
    public partial class ManagePost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PostService service = new PostService();
            PostGridView.DataSource = service.GetAllPosts("");
            PostGridView.DataBind();
        }

        protected void PostGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= PostGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (PostGridView.PageIndex + 1).ToString();
            }
        }

        protected void PostGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("EditPost"))
            {
                Response.Redirect(string.Format("EditPost.aspx?{0}={1}", Constants.QUERYSTR_POST_ID, e.CommandArgument));
            }
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            PostGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            PostService service = new PostService();
            PostGridView.DataSource = service.GetAllPosts("");
            PostGridView.DataBind();
        }

        protected void PostGridView_DataBound(object sender, EventArgs e)
        {
            if (PostGridView.BottomPagerRow != null)
            {
                PostGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void PostGridView_PageIndexChanged(object sender, EventArgs e)
        {
        }

        protected void PostGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < PostGridView.PageCount)
            {
                PostGridView.PageIndex = e.NewPageIndex;
            }
            
            PostService service = new PostService();
            PostGridView.DataSource = service.GetAllPosts("");
            PostGridView.DataBind();
        }
    }
}
